package org.seed.example.democonsumer;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.seed.example.demoprovider.RawTradeDeliverPushRequest;
import org.seed.example.demoprovider.StudentService;
import org.seed.example.demoprovider.TeacherService;
import org.seed.rpc.core.RpcReference;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoRpcController {

    @RpcReference
    private StudentService studentService;
    @RpcReference
    private TeacherService teacherService;

    @GetMapping("/test")
    public String testStudentAndTeacher() {
        long currentMillisecond = System.currentTimeMillis();
        log.info("Student Name: {}, {}ms", studentService.getMyName(), System.currentTimeMillis() - currentMillisecond);
        currentMillisecond = System.currentTimeMillis();
        log.info("Student Score: {}, {}ms", studentService.getMyScore(), System.currentTimeMillis() - currentMillisecond);
        currentMillisecond = System.currentTimeMillis();
        log.info("Teacher classes: {}, {}ms", new Gson().toJson(teacherService.getMyClasses()), System.currentTimeMillis() - currentMillisecond);
        return "OK";
    }


    @GetMapping("/timeout")
    public String testTimeout() {
        log.info("Timeout: {}", new Gson().toJson(teacherService.timeout()));
        return "Called Success";
    }

    @GetMapping("/date-param")
    public String dateParam() {
        studentService.checkDateParam(new Date());
        return "Called Success";
    }

    @GetMapping("/duplex-param")
    public String testDuplexParam() {
        String paramStr = "[{\"shopNo\":\"6003\",\"tid\":\"10202303020852000004\",\"orderCount\":1,\"goodsCount\":1,\"payMethod\":7,\"payTime\":\"2023-03-02 08:52:16\",\"buyerNick\":\"yeah\",\"receiverName\":\"雷晓\",\"receiverArea\":\"山东省 聊城市 东阿县\",\"receiverAddress\":\"环球路丝绸公司对过\",\"receiverMobile\":\"18365871989\",\"postAmount\":0,\"discount\":0,\"receivable\":0,\"invoiceType\":0,\"remarkFlag\":0,\"rawTradeOrderList\":[{\"oid\":\"10202303020852000004-1\",\"goodsId\":\"1742\",\"specId\":\"1742\",\"goodsNo\":\"2001010312-00006\",\"specNo\":\"6958862117715\",\"goodsName\":\"清口香夹心方骨荷兰芹小号100g\",\"num\":1,\"price\":0,\"discount\":0,\"shareDiscount\":0,\"totalAmount\":0,\"adjustAmount\":0,\"refundAmount\":0,\"remark\":\"\"}],\"rawTradeDiscountList\":[]}]";
        List<RawTradeDeliverPushRequest> deliverPushRequestList = new Gson().fromJson(paramStr, new TypeToken<List<RawTradeDeliverPushRequest>>() {
        }.getType());
        deliverPushRequestList.get(0).setTradeTime(new Date());
        teacherService.pushDeliveryInfo(deliverPushRequestList);
        return "OK";
    }

}
